package org.jeecg.modules.compilation.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarUtil {

    /**
     * 获取指定时间当周星期一的时间
     * @param time 指定时间 yyy-MM-dd
     * @return yyy-MM-dd
     */
    public static String getNowWeek(String time) throws ParseException {
        Calendar now = Calendar.getInstance();
        now.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(time));
        int weekDay = now.get(Calendar.DAY_OF_WEEK);
        if( now.getFirstDayOfWeek() == Calendar.SUNDAY ){
            weekDay = weekDay - 1;
            if(weekDay == 0){
                weekDay = 7;
            }
        }

        int year = now.get(Calendar.YEAR);
        int month = (now.get(Calendar.MONTH)+1);
        int day = now.get(Calendar.DAY_OF_MONTH);

        if(weekDay >= day){
            if(month == 1){
                year -= 1;
                month = 12;
                day = 31 + day - weekDay;
            }else{
                month -= 1;
                //获取该月总天数
                day = Integer.parseInt(sumMonthDay(year+"-"+month)) + day - weekDay;
            }
        } else{
            day -= weekDay;
        }
        return year+"-"+ ( month < 10 ? "0" + month : month + "") +"-"+ ( ( day + 1 ) < 10 ? "0" + ( day + 1 ) : ( day + 1 ) + "") ;
    }
    /**
     * 获取当前时间的本周的星期一的时间
     * @return yyy-MM-dd
     */
    public static String getNowWeek() throws ParseException {
            return getNowWeek( getNowYear() + "-" + getNowMonth() + "-" + getNowDay() );
    }
    /**
     * 传入指定时间格式获取当前时间
     * @param pattern 时间格式
     * */
    public static String  getNowTime(String  pattern){
        return new SimpleDateFormat(pattern).format(Calendar.getInstance().getTime());
    }

    /**
     * 获取当年
     * */
    public static String getNowYear()   {
      return  Calendar.getInstance().get(Calendar.YEAR) + "";
    }
    /**
     * 获取当月
     * */
    public static String getNowMonth()  {
       return  Calendar.getInstance().get(Calendar.MONTH) < 10 ? "0" + ( Calendar.getInstance().get(Calendar.MONTH) + 1 ) : ( Calendar.getInstance().get(Calendar.MONTH) + 1 ) + "" ;
    }
    /**
     *获取当日
     */
    public static String getNowDay() throws ParseException {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH) < 10 ? "0" + Calendar.getInstance().get(Calendar.DAY_OF_MONTH) : Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + "";
    }

    public static String getNowTimeYYYYMMDD() throws ParseException {
        return getNowYear() + "-" + getNowMonth() + "-" + getNowDay() + "";
    }

    /**
     * 格式：yyyy-MM
     * 获取该月总天数
     * @param pattern 指定时间
     * */
    public static String  sumMonthDay(String pattern) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(pattern));
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH) < 9 ? "0" + calendar.getActualMaximum(Calendar.DAY_OF_MONTH)  : calendar.getActualMaximum(Calendar.DAY_OF_MONTH) +"";
    }


    /**
     * 计算指定日期的往前n个月的开始日期   日期格式：yyyy-MM-dd
     * @param startTime 指定日期
     * @param n 往前推 n 个月
     * */
    public static String  ForwardCalculationMonth(String startTime, int n) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(startTime));
        int startyear = calendar.get(Calendar.YEAR);
        int startMonth = (calendar.get(Calendar.MONTH)+1);
        int startDay = calendar.get(Calendar.DAY_OF_MONTH);

        int endYear = n < startMonth ? startyear :  ( n % 12 ) >= startMonth ?   startyear - ( n / 12 ) - 1 :  startyear - ( n /12 ) ;
        int endMonth = ( n % 12 ) >= startMonth ?  ( 12 + startMonth ) - n % 12 : startMonth - n % 12;
        int endDay = Integer.parseInt(sumMonthDay(endYear+"-"+endMonth));
        //不需要讨论是否为闰年
        if(startDay > endDay){
            endMonth += 1;
            //不需要分类 endMonth + 1 > 12, 因为12月是大月
            endDay = startDay - endDay ;
        } else {
            endDay = startDay;
        }
        String resultEndMonth = Integer.parseInt(String.valueOf(endMonth)) < 9 ? "0" + endMonth + "" : endMonth + "";
        String resultEndDay = Integer.parseInt(String.valueOf(endDay)) < 9 ? "0" + endDay + "" : endDay + "";
        return endYear+"-" + resultEndMonth + "-" + resultEndDay;
    }


    /**
     * 计算当前日期开始往前n个月的开始日期   日期格式：yyyy-MM-dd
     * @param n 当前时间往前推 n 个月
     *
     * */
    public static String  ForwardCalculationMonth(int n) throws ParseException {
        return ForwardCalculationMonth(getNowYear()+"-"+getNowMonth()+"-"+getNowDay() ,n);
    }



//    public static void main(String[] args) throws ParseException {
//        String name = "0";
//        System.out.println(name == null || "".equals(name) || name.equals("1"));
//
////        Long currentTime = System.currentTimeMillis();
////        System.out.println("Current Time : " + currentTime + " = " + timestampToStr(currentTime, "GMT+8"));
////        Long dailyStart = getDailyStartTime(currentTime, "GMT+8:00");
////        Long dailyEnd = getDailyEndTime(currentTime, "GMT+8:00");
////        Long monthStart = getMonthStartTime(currentTime, "GMT+8:00");
////        Long monthEnd = getMonthEndTime(currentTime, "GMT+8:00");
////        Long yearStart = getYearStartTime(currentTime, "GMT+8:00");
////        Long yearEnd = getYearEndTime(currentTime, "GMT+8:00");
////
////        System.out.println("Daily Start : " + dailyStart + " = " + timestampToStr(dailyStart, "GMT+8") + " Daily End : " + dailyEnd + " = " + timestampToStr(dailyEnd, "GMT+8"));
////        System.out.println("Month Start : " + monthStart + " = " + timestampToStr(monthStart, "GMT+8") + " Month End : " + monthEnd + " = " + timestampToStr(monthEnd, "GMT+8"));
////        System.out.println("Year Start : " + yearStart + " = " + timestampToStr(yearStart, "GMT+8") + " Year End : " + yearEnd + " = " + timestampToStr(yearEnd, "GMT+8"));
//    }


}
